package com.personalsystem.role.action;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.personalsystem.function.bean.Functions;
import com.personalsystem.function.dao.FunctionDao;
import com.personalsystem.role.bean.Role;
import com.personalsystem.role.dao.RoleDao;
import com.personalsystem.role.service.RoleService;
import com.personalsystem.role.util.RoleUtil;
import com.personalsystem.tool.BackJson;
import com.personalsystem.tool.hibernate.Updater;
import com.personalsystem.tool.hibernate.Updater.UpdateMode;
import com.personalsystem.tool.page.Page;



@Controller
public class RoleAction {
	
	@Autowired
	@Qualifier("roleService")
	private RoleService service;
	
	@Autowired
	@Qualifier("roleDao")
	private RoleDao dao;
	
	@Autowired
	@Qualifier("functionDao")
	private FunctionDao functionDao;
	
	@RequestMapping("/role/loadTree.do")
	public void loadTree(HttpServletRequest request,HttpServletResponse response){
		List<Functions> list=functionDao.getList();
		String jsonString=new RoleUtil().getTreeJson(list,null);
		System.out.println(jsonString);
		BackJson.backJsonToHTML(response, "["+jsonString+"]");
	}
	
	@RequestMapping("/role/loadTreeById.do")
	public void loadTreeById(long id,HttpServletRequest request,HttpServletResponse response){
		List<Functions> list=functionDao.getList();
		Role role=service.findById(id);
		Set<Functions> set=role.getFunSet();
		List<Long> longList=new ArrayList<Long>();
		Iterator<Functions> iterator=set.iterator();
		while(iterator.hasNext()){
			longList.add(iterator.next().getId());
		}
		String jsonString=new RoleUtil().getTreeJson(list,longList);
		System.out.println(jsonString);
		BackJson.backJsonToHTML(response, "["+jsonString+"]");
	}
	
	
	@RequestMapping("/role/loadTable.do")
	public void loadRoleTable(int start,int limit,HttpServletRequest request,HttpServletResponse response){
		Page page=dao.getPage(start+1, limit);
		String jsonString=new RoleUtil().getTableJson(page);
		System.out.println(jsonString);
		BackJson.backJsonToHTML(response, jsonString);
	}
	
	@RequestMapping("/role/addRole.do")
	public void addRole(Role role,String funIdx,HttpServletRequest request,HttpServletResponse response){
		String[] strings=funIdx.split(",");
		Set<Functions> set=new HashSet<Functions>();
		for(String s:strings){
			if(!s.equals("")){
				Functions functions=functionDao.get(Long.parseLong(s));
				set.add(functions);
			}
		}
		role.setFunSet(set);
		role.setRemark(Jsoup.parse(role.getRemarkUbb()).text());
		if(role.getId()==null){
			role.setStatus(1);
			service.save(role);
		}else{
			Updater<Role> updater=new Updater<Role>(role,UpdateMode.MIDDLE);
			service.updateByUpdater(updater);
		}
		
		//service.saveXX();
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}
	
	@RequestMapping("/role/loadRole.do")
	public void loadRole(long id,HttpServletRequest request,HttpServletResponse response){
		Role role=service.findById(id);
		Map map=new TreeMap();
		map.put("success", true);
		map.put("data", role);
		JSONObject object=new JSONObject();
		object.putAll(map);
		BackJson.backJsonToHTML(response, object.toString());
	}
	
	@RequestMapping("/role/updateRole.do")
	public void updateRole(Role role,HttpServletRequest request,HttpServletResponse response){
		service.update(role);
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}
	
	@RequestMapping("/role/deleteRole.do")
	public void deleteRole(String idx,HttpServletRequest request,HttpServletResponse response){
		String[] arr=idx.split(",");
		List<Long> list=new ArrayList<Long>();
		for(String s:arr){
			if(!s.equals("")){
				list.add(Long.parseLong(s));
			}
		}
		Long[] lArr=new Long[list.size()];
		for(int i=0;i<list.size();i++){
			lArr[i]=list.get(i);
		}
		service.deleteById(lArr);
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}

}
